package com.hhkj.admin.bank.common.component.creator;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hhkj.admin.bank.common.cont.RuleConst;
import com.hhkj.admin.bank.domain.BankRule;
import com.hhkj.admin.bank.model.req.BankRuleReq;

import java.util.ArrayList;
import java.util.List;

/**
 * RuleCreator
 *
 * @author ZhengJun
 * @since 2023/6/11 13:18
 */
public class RuleCreator
{

    /**
     * 创建科目抽屉规则列表
     *
     * @return 抽屉规则列表
     */
    public static List<BankRule> createChildren(BankRuleReq req)
    {
        List<BankRule> resultList = new ArrayList<>();
        if (ObjectUtil.isNull(req.getId())) {
            return resultList;
        }
        if (!req.hasCourseChildrenRule()) {
            return resultList;
        }

        boolean isPractiseDifferent
                = RuleConst.ConfModeType.DIFFERENCE.getCode().equals(req.getPracticeConfModeTypeCode());
        boolean isTestDifferent
                = RuleConst.ConfModeType.DIFFERENCE.getCode().equals(req.getTestConfModeTypeCode());

        BankRule childDo;
        for (BankRuleReq childRule : req.getChildren()) {

            childDo = BeanUtil.toBean(childRule, BankRuleReq.class);
            childDo.setId(null);
            // 初始值
            childDo.setTypeCode(RuleConst.Type.SINGLE.getCode());
            childDo.setCourseBankTypeId(req.getCourseBankTypeId());
            childDo.setCourseParentId(req.getCourseParentId());
            childDo.setParentId(req.getId());
            childDo.setPracticeConfModeTypeCode(RuleConst.ConfModeType.UNIFY.getCode());
            childDo.setTestConfModeTypeCode(RuleConst.ConfModeType.UNIFY.getCode());
            if (!isPractiseDifferent) {
                childDo.copyPractiseProperties(req);
            }
            if (!isTestDifferent) {
                childDo.copyTestProperties(req);
            }
            resultList.add(childDo);
        }
        return resultList;
    }
}
